package com.sulj.service;

import com.sulj.domain.CartItem;
import com.sulj.domain.Order;

import java.math.BigDecimal;

/**
 * 购物服务类
 *
 * @author slj
 * @date 2025-03-29
 */
public class ShoppingService {


    /**
     * 计算本次购物总价
     *
     * @return 总价
     */
    public BigDecimal calculateGrandTotal(Order order) {
        BigDecimal total = order.getItems().stream()
                .map(CartItem::getCartItemTotal)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        order.setTotalAmount(total);
        if (order.getCoupon() != null) {
            order.setTotalAmount(order.getCoupon().applyDiscount(order));
        }
        return order.getTotalAmount();
    }

}
